package com.demo.base.ega;

import com.demo.result.exception.RRException;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author : YU.TAN
 * @date : 2021/8/26 15:40
 */
@Slf4j
@Service
public class SyncJobHandler {

    private List<SyncJob> jobs=new ArrayList<>(20);

    @Autowired
    ApplicationContext context;

    @PostConstruct
    public void initJobs(){
        Map<String, SyncJob> beansOfType = context.getBeansOfType(SyncJob.class);
        jobs.addAll(beansOfType.values());
    }

    @SneakyThrows
    public Object doSync(SyncParams params)  {
        SyncJob job = jobs.stream().filter(syncJob -> syncJob.jobType() == params.getSyncEnum())
                .findFirst()
                .orElseThrow(() -> new RRException("syncEnum Illegal !"));
        return job.doSync(params);
    }
}
